package miku.firstgame.world
{
	import com.saia.starlingPunk.SPEntity;
	import com.saia.starlingPunk.SPWorld;
	import com.saia.starlingPunk.extensions.ogmopunk.OgmoProject;

	import flash.display.Bitmap;
	import flash.geom.Point;
	import flash.utils.Dictionary;

	import miku.firstgame.entity.Bomb;
	import miku.firstgame.entity.FireEntity;
	import miku.firstgame.entity.TestPlayer;
	import miku.firstgame.entity.TestTank;
	import miku.firstgame.event.PlayerEvent;

	import starling.text.TextField;
	import starling.utils.HAlign;

	public class TestWorld extends SPWorld
	{
		[Embed(source = "media/testPunk.oep", mimeType = "application/octet-stream")]
		public const OGMO_PROJECT:Class;

		[Embed(source = "media/Level1.oel", mimeType = "application/octet-stream")]
		public const LEVEL01:Class;

		[Embed(source = "media/tanks_sheet.png")]
		public const map:Class;

		private var player:TestPlayer;

		public function TestWorld()
		{
			super();
			TestPlayer;
			TestTank;
		}

		override public function begin():void
		{
			//load the embedded project file 
			OgmoProject.LoadProject(OGMO_PROJECT, this as Class);
			//var textureAtlas:TextureAtlas = new TextureAtlas(Texture.fromBitmap(new ExampleAssets.ATLAS_TEXTURE()), XML(new ExampleAssets.ATLAS_DATA()));
			//before loading the level you must tell OgmoPunk which images to use for the tileset
			//OgmoProject.createTileSetListFromTextureAtlas("groundTileSet", textureAtlas, ["platformTile", "marioTile"]);

			OgmoProject.createTilesFromBitmapData("tank", (new map() as Bitmap).bitmapData, 32, 32);

			super.begin();

//			var textField:TextField = new TextField(200, 100, "");
//			textField.text = "use numbers keys (1-3) to see other examples ";
//			textField.hAlign = HAlign.LEFT; 
// 
//			addChildAt(textField, this.numChildren);
//			textField.x = 20;
//			textField.y = 40;

			loadLevel(LEVEL01);

		}

		protected function loadLevel(levelData:Class):void
		{
			var levelLayers:Vector.<SPEntity> = OgmoProject.LoadLevel(levelData);
			for each (var entity:SPEntity in levelLayers)
			{
				add(entity);

				if (entity is TestPlayer)
				{
					player = entity as TestPlayer;
					entity.addEventListener(PlayerEvent.PLACE_BOMB, onPlaceBomb);
				}
			}
		}

		private function onPlaceBomb(e:PlayerEvent):void
		{
			var xCell:int = Math.round(player.x / 32);
			var yCell:int = Math.round(player.y / 32);

			var bomb:Bomb = new Bomb();
			bomb.x = xCell * 32;
			bomb.y = yCell * 32;
			bomb.addEventListener(PlayerEvent.EXPLODE, onBombExplode);
			add(bomb);
		}

		private function onBombExplode(e:PlayerEvent):void
		{

			var bomb:Bomb = e.target as Bomb;

			var fireDic:Dictionary = new Dictionary();
			fireDic[bomb.x + "-" + bomb.y] = true;

			var point2:Point = new Point();

			var canTop:Boolean = true;
			var canBottom:Boolean = true;
			var canLeft:Boolean = true;
			var canRight:Boolean = true;

			for (var i:int = 1; i <= bomb.energy; i++)
			{
				if (canTop)
				{
					point2.x = bomb.x;
					point2.y = bomb.y - 32 * i;
					if (point2.x >= 0 && point2.y >= 0 && point2.x < 10 * 32 && point2.y < 10 * 32)
					{
						if (!bomb.collide("collision", point2.x, point2.y))
						{
							fireDic[point2.x + "-" + point2.y] = true;
						}
						else
						{
							canTop = false;
						}
					}
					else
					{
						canTop = false;
					}
				}

				if (canBottom)
				{
					point2.x = bomb.x;
					point2.y = bomb.y + 32 * i;

					if (point2.x >= 0 && point2.y >= 0 && point2.x < 10 * 32 && point2.y < 10 * 32)
					{
						if (!bomb.collide("collision", point2.x, point2.y))
						{
							fireDic[point2.x + "-" + point2.y] = true;
						}
						else
						{
							canBottom = false;
						}
					}
					else
					{
						canBottom = false;
					}
				}

				if (canLeft)
				{
					point2.x = bomb.x - 32 * i;
					point2.y = bomb.y;

					if (point2.x >= 0 && point2.y >= 0 && point2.x < 10 * 32 && point2.y < 10 * 32)
					{
						if (!bomb.collide("collision", point2.x, point2.y))
						{
							fireDic[point2.x + "-" + point2.y] = true;
						}
						else
						{
							canLeft = false;
						}
					}
					else
					{
						canLeft = false;
					}
				}

				if (canRight)
				{
					point2.x = bomb.x + 32 * i;
					point2.y = bomb.y;

					if (point2.x >= 0 && point2.y >= 0 && point2.x < 10 * 32 && point2.y < 10 * 32)
					{
						if (!bomb.collide("collision", point2.x, point2.y))
						{
							fireDic[point2.x + "-" + point2.y] = true;
						}
						else
						{
							canRight = false;
						}
					}
					else
					{
						canRight = false;
					}
				}
			}

			for (var key:String in fireDic)
			{
				var fire:FireEntity = new FireEntity();
				var point:Array = key.split("-");
				fire.x = point[0];
				fire.y = point[1];
				add(fire);
			}

			remove(bomb);
		}
	}
}
